/*
 * Copyright 2010 dev7.jp
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.dev7.ao;

import java.sql.SQLException;
import java.util.Map;

import jp.co.apl.haven.ao.exception.DaoException;
import net.java.ao.DBParam;
import net.java.ao.Entity;
import net.java.ao.EntityManager;
import net.java.ao.Query;
import net.java.ao.RawEntity;

import com.google.inject.Inject;

public abstract class Dao<T extends Entity> {

    @Inject
    protected EntityManager entityManager;

    public <K> int count() {
        try {
            return entityManager.count(type());
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public <K> int count(Query query) {
        try {
            return entityManager.count(type(), query);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public <K> int count(String criteria, Object... parameters) {
        try {
            return entityManager.count(type(), criteria, parameters);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public T create(DBParam... params) {
        try {
            return entityManager.create(type(), params);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public T create(Map<String, Object> params) {
        try {
            return entityManager.create(type(), params);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public void delete(RawEntity<?>... entities) {
        try {
            entityManager.delete(entities);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public T[] find() {
        try {
            return entityManager.find(type());
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public T[] find(Query query) {
        try {
            return entityManager.find(type(), query);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public T[] find(String criteria, Object... parameters) {
        try {
            return entityManager.find(type(), criteria, parameters);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public T[] find(String field, Query query) {
        try {
            return entityManager.find(type(), field, query);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public T[] findWithSQL(String keyField, String sql, Object... parameters) {
        try {
            return entityManager.findWithSQL(type(), keyField, sql, parameters);
        } catch (SQLException e) {
            throw new DaoException(e);
        }
    }

    public T[] get(Integer... keys) {
        return entityManager.get(type(), keys);
    }

    public T get(Integer key) {
        return entityManager.get(type(), key);
    }

    protected abstract Class<T> type();

}
